﻿using System.Diagnostics;

namespace TPXSDK.NET.Mission;

public class FlyCmdItem
{
    public byte Id { get; set; }
    public byte P1 { get; set; }
    public short P2 { get; set; }
    public AnyValue P3 { get; set; }
    public AnyValue P4 { get; set; }
    public int LineNum { get; set; }

    public FlyCmdItem()
    {
        P1 = 0;
        P2 = 0;
        P3 = new AnyValue();
        P4 = new AnyValue();
    }

    /// <summary>
    /// 将对象转换为字节数组
    /// </summary>
    public byte[] GetBytes()
    {
        var bytes = new byte[12];
        bytes[0] = Id;
        bytes[1] = P1;
        var tmp = BitConverter.GetBytes(P2);
        bytes[2] = tmp[0];
        bytes[3] = tmp[1];
        for (var i = 0; i < 4; i++)
        {
            bytes[4 + i] = P3.Bytes[i];
            bytes[8 + i] = P4.Bytes[i];
        }
        return bytes;
    }

    /// <summary>
    /// 从字节数组获得成员变量值
    /// </summary>
    public void SetBytes(byte[] bytes, int offset = 0)
    {
        Debug.Assert(bytes.Length - offset == 12);
        Id = bytes[offset + 0];
        P1 = bytes[offset + 1];
        P2 = BitConverter.ToInt16(bytes, offset + 2);

        var tmp = new byte[4];
        Array.Copy(bytes, offset + 4, tmp, 0, 4);
        P3.Bytes = tmp;
        Array.Copy(bytes, offset + 8, tmp, 0, 4);
        P4.Bytes = tmp;
    }
}
